<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<script>
  // function combine (...arg) {
  //   return arg.reduce((prev, next) => {
  //     return (args, __next) => prev(args, () => next(args, __next))
  //   })
  // }

  const compose = (...fns) => (arg) => fns.reduceRight((acc, fn) => {
    /**
     * 将参数传递给最后一个函数
     */
    return (fn ? fn(acc) : acc)
  }, arg)

  const add = (num1, next) => {
    // next()
    console.log('add: => ', num1, next)
    return num1
  }
  const multiply = (num1, next) => {
    // next()
    console.log('multiply: => ', num1, next)
    return num1
  }
  const subtract = (num1, next) => {
    console.log('subtract: => ', num1, next)
    return num1
  }

  const composedOperations = compose(add, multiply, subtract)

  composedOperations(0)

  // const compute = (arr, initialNum = 0) => {
  //   return arr.reduce((acc, val) => composedOperations(acc), initialNum)
  // }
  //
  // console.log(compute([-10, 25, 55, 22], 6))
</script>
</body>
</html>